"""
竞赛用户答题
"""

import time

from selenium import webdriver
from datetime import datetime, date, timedelta
from faker import Faker
import random

from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

start = time.time()

fake = Faker("zh_CN")

option = webdriver.ChromeOptions()
wd = webdriver.Chrome()

wd.get("http://172.16.60.14")
wd.maximize_window()
wd.implicitly_wait(10)


#用户中心登录
# 点击登录按钮
wd.find_element_by_xpath("//../span[text() = '用户中心账号']").click()

# 输入邮箱
wd.find_element_by_xpath("//input[@placeholder='邮箱/账号ID']").send_keys("user6@123.com")

# 输入密码
wd.find_element_by_xpath("//input[@placeholder = '密码']").send_keys("123456")

wd.find_element_by_xpath("//span[text() = '授权并登录']").click()

# 系统用户登录
# # 输入邮箱
# wd.find_element_by_xpath("//input[@placeholder='请输入用户名']").send_keys("c9")
#
# # 输入密码
# wd.find_element_by_xpath("//input[@placeholder = '请输入密码']").send_keys("1")

# # 点击“授权并登录”按钮
# wd.find_element_by_xpath("//span[text() = '登录']").click()

try:
    # 存在参赛须知
    wd.find_element(By.XPATH, '//div[@aria-label="参赛须知"]')
    time.sleep(11)
    # 勾选确认框
    wd.find_element(By.XPATH, '//span[@class="el-checkbox__label"]').click()
    wd.find_element(By.XPATH, '//span[text()="确认提交"]').click()

except NoSuchElementException:
    print("没有参赛须知框")


# 统计题目模块总数
eles = wd.find_elements(By.XPATH, '//div[@class="f-toe-line-2 f-m-r-20"]')
print("题目总数: "+str(len(eles)))


# 点击第一题
eles[0].click()

x = 1
for i in range(0,len(eles)):

    # 题目类型
    type =  wd.find_element(By.XPATH, '//span[@class="f-fwb f-fc-link"]').text

    if "填空" in type:
        wd.find_element(By.XPATH, '//textarea').send_keys("飞 客")
    elif "单选" in type:
        wd.find_element(By.XPATH, '//div[@class = "el-radio-group"]//input[@value="A"]/..').click()
    elif "多选" in type:
        wd.find_element(By.XPATH, '//input[@value="A"]/..').click()
        wd.find_element(By.XPATH, '//input[@value="B"]/..').click()
    elif "主观" in type:
        wd.find_element(By.XPATH, '//textarea').send_keys("我是这个苍井空三的寄快递")



    wd.find_element(By.XPATH, '//span[text()="提交答案"]').click()
    time.sleep(2)

    x += 1
    if x % 10 == 0:
        time.sleep(10)

# 结束答题按钮
wd.find_element(By.XPATH, '//span[text()="结束答题"]/..').click()


# 提示的本次竞赛的名称
name = wd.find_element(By.XPATH, '//span[@class="f-fwb f-fc-333 f-m-r-10"]')



# 结束答题输入框
wd.find_element(By.XPATH, '//input[@placeholder="请输入本次竞赛名称"]').send_keys(name.text)

wd.find_element(By.XPATH, '//span[text()="确定"]').click()





end = time.time()

print("run_time: "+str(end - start))


time.sleep(10)

wd.quit()